#encoding:utf8

import time

from selenium import webdriver
from selenium.webdriver.common import desired_capabilities

USE_CHROME = 0
USE_FIREFIX = 0
USE_PHANTOMJS = 0

def get_cookie():
    if True:
        raise NotImplementedError("You must implement the code to generate cookie dict and return")

    if USE_CHROME:
        chrome_path = ""
        driver = webdriver.Chrome(executable_path=chrome_path)

    if USE_FIREFIX:
        firefox_path = ""
        driver = webdriver.Firefox(executable_path=firefox_path)

    if USE_PHANTOMJS:
        ############# use phantomjs
        cap = webdriver.DesiredCapabilities.PHANTOMJS
        cap["browserName"] = "firefox"
        cap["phantomjs.page.settings.resourceTimeout"] = 1000
        cap["phantomjs.page.settings.loadImages"] = False
        cap["phantomjs.page.settings.disk-cache"] = True
        # cap["phantomjs.page.customHeaders.Cookie"] = 'SINAGLOBAL=3955422793326.2764.1451802953297; ' #我删掉了一大部分
        cap[
            "phantomjs.page.customHeaders.userAgent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13"
        cap[
            "phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13"

        # TODO
        phantomjs_path = "/Users/mavarick/softwares/phantomjs-2.1.1-macosx/bin/phantomjs"
        driver = webdriver.PhantomJS(executable_path=phantomjs_path)

    driver.set_page_load_timeout(60)

    login_url = "https://www.itjuzi.com/user/login"
    driver.get(login_url)

    identity = driver.find_element_by_id("create_account_email")
    identity.send_keys("mavarick@163.com")
    passwd = driver.find_element_by_id("create_account_password")
    passwd.send_keys("mavarick2016")
    btn = driver.find_element_by_id("login_btn")
    btn.click()

    time.sleep(10)

    # check if login
    if driver.current_url == login_url:
        raise Exception("can not login")

    # get cookie
    cookie = [item["name"] + "=" + item["value"] for item in driver.get_cookies()]
    cookiestr = ';'.join(item for item in cookie)



############# use cookie to connect

# headers = {
#     "Host": "www.itjuzi.com",
#     "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:49.0) Gecko/20100101 Firefox/49.0",
#     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
#     "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
#     "Accept-Encoding": "gzip, deflate, br",
#     "Cookie": "_ga=GA1.2.2057419966.1476352888; gr_user_id=ad27cc0d-66bd-42ad-9198-53b086c945f7; Hm_lvt_1c587ad486cdb6b962e94fc2002edf89=1479733227,1479734485,1479741599,1479741613; Hm_lpvt_1c587ad486cdb6b962e94fc2002edf89=1479746043; session=23114010507a7d96d768405584d71871782a74c3; identity=mavarick%40163.com; remember_code=0nHvvXk5Ub; gr_session_id_eee5a46c52000d401f969f4535bdaa78=4d0521d4-41a4-42de-8720-74f45e292d57; _gat=1",
#     "Connection": "keep-alive"
# }
#
# import requests
# s = requests.Session()
# s.headers.update(headers)
# url = "http://www.itjuzi.com/company?scope=12&sub_scope=21&born_year=2016&sortby=foundtime&page=2"
# s.get(url)

